#!/bin/bash
if [ $# -lt 2 ]; then                                                                                                                               
	exit 1                                                                                                                                                                                                         
fi                                                                                                                                                                                                                                                                                                                                                                                                                                                              # 定义变量                                                                                                                                                                                      
string=""                                                                                                                                                                                                         
current_parent_dir_name=""                                                                                                                                                                                        
flag=0                                                                                                                                                                                                            
slash="/"                                                                                                                                                                                                         
option="-x"                                                                                                                                                                                                       
a="-"                                                                                                                                                                                                                                                                                                                                                                                                                               
# 不为还原选项                                                                                                                                                                                                    
if [ ! $1 = $option ]; then                                                                                                                                                                                           
	parent_dir_name=$1                                                                                                                                                                                                
	ls -R $1 | while read line; do                                                                                                                                                                                
#    echo "读入的是:$line"                                                                                                                                                                                           	
	 # 获取父目录名字                                                                                                                                                                                                  
	 if [ $flag -eq 0 ]; then                                                                                                                                                                                              
	 	flag=1                                                                                                                                                                                                            
	 	length=${#line}                                                                                                                                                                                                   
	 	let length_1=$length-1                                                                                                                                                                                            
	 	current_parent_dir_name=${line:0:$length_1}                                                                                                                                                               
#        echo "当前父目录名字:$current_parent_dir_name"                                                                                                                                                               
	 else                                                                                                                                                                                                                  
	 	file_name="$current_parent_dir_name$slash$line"                                                                                                                                                           
#        echo "文件名:$file_name"                                                                                                                                                                                         
	 	# 如果是普通文件                                                                                                                                                                                                  
	 	if [ -f $file_name ]; then                                                                                                                                                                                
#            echo "普通文件, line=$line"                                                                                                                                                                          #            echo "当前父目录名字:$current_parent_dir_name"                                                                                                                                                                   
	 		for (( i=0; i<${#current_parent_dir_name}; i++)); do                                                                                                                                                                  
	 			char=${current_parent_dir_name:$i:1}                                                                                                                                                                              
	 			if [ $char = $slash ]; then                                                                                                                                                                                           	
	 				char="-"                                                                                                                                                                                                      
	 			fi                                                                                                                                                                                                                
	 			string=${string}${char}                                                                                                                                                                                       
	 		done                                                                                                                                                                                                  
#            echo "当前父目录名字:=$current_parent_dir_name 拼接后父目录名字:$string"                                                                                                                                         
	 		# 主目录下面拷贝过去, 直接拷贝                                                                                                                                                                                    
	 		if [ $parent_dir_name = $string ]; then                                                                                                                                                                               
	 			string="$2$slash$line"                                                                                                                                                                          #                echo "string=$string"                                                                                                                                                                                            
	 			cp $file_name $string                                                                                                                                                                                         
	 			# 子目录拷贝过去, 文件名改变                                                                                                                                                                                      
	 		else                                                                                                                                                                                                                  
	 		# 除去主目录的名称                                                                                                                                                                                
#                echo "主目录的名称:$parent_dir_name, 长度:${#parent_dir_name}"                                                                                                                                   
#                echo "前:$string"                                                                                                                                                                                                
				string=${string:${#parent_dir_name}+1:${#string}}                                                                                                                                                 
#                echo "后:$string"                                                                                                                                                                                                	
				# 目录名-文件名                                                                                                                                                                                                   
				string="${string}${a}${line}"                                                                                                                                                                                     
				# 拷贝目录名 / 拷过去要改的名字                                                                                                                                                                                   
				string="$2$slash$string"                                                                                                                                                                          
#                echo "cp $file_name $string"                                                                                                                                                                                     
				cp $file_name $string                                                                                                                                                                                         		
			fi                                                                                                                                                                                                            
				# 如果是空 ""                                                                                                                                                                                                     
			elif [ ${#line} -eq 0 ]; then                                                                                                                                                                                        
				flag=0                                                                                                                                                                                                 #           echo "flag置换"                                                                                                                                                                                               
			fi                                                                                                                                                                                                                
			string=""                                                                                                                                                                                                     
		fi                                                                                                                                                                                                            
	done                                                                                                                                                                                                              
# 还原选项                                                                                                                                                                                                        
elif [ $1 = $option ]; then                                                                                                                                                                                           
	parent_dir_name=$3                                                                                                                                                                                                
	index=0                                                                                                                                                                                                           
	ls $2 | while read line; do                                                                                                                                                                                           
	# 遍历文件名, 找出目录和文件名                                                                                                                                                                            
#        echo "参数长度:${#line}"                                                                                                                                                                                         
	for(( i=0; i<${#line}; i++)); do                                                                                                                                                                                      
	char=${line:$i:1}                                                                                                                                                                                     
#            echo "char=$char"                                                                                                                                                                                                
		if [ $char = $a ]; then                                                                                                                                                                                               
			array[$index]=$string                                                                                                                                                                             
#                echo "array[$index]=${array[$index]}"                                                                                                                                                                            
			let index=$index+1                                                                                                                                                                                                
			string=""                                                                                                                                                                                                     
		else                                                                                                                                                                                                                  
			string="$string$char"                                                                                                                                                                                         
		fi                                                                                                                                                                                                            
	done                                                                                                                                                                                                              
	array[$index]=$string                                                                                                                                                                                     
#       echo "array[$index]=${array[$index]}"                                                                                                                                                                            
	file_name=${array[$index]}                                                                                                                                                                                        
	string=""                                                                                                                                                                                                         
	for (( i=0; i<$index; i++)); do                                                                                                                                                                                       
		let s=$i-1                                                                                                                                                                                                        
		# 拼接成要还原的目录下的路径                                                                                                                                                                                      
		if [ $i -eq 0 ]; then                                                                                                                                                                                                 
			string=$parent_dir_name$slash${array[$i]}                                                                                                                                                                     
		else
#			echo "前: $string"                                                                                                                                                                                                               
            string=$string$slash${array[$i]}
#			echo "后: $string"                                                                                                                                               
		fi                                                                                                                                                                                                    
#            echo "路径:$string"                                                                                                                                                                                              
		# 目录不存在就创建                                                                                                                                                                                                
		if [ ! -d $string ]; then                                                                                                                                                                             
#                echo "mkdir $string"                                                                                                                                                                                             
			mkdir $string                                                                                                                                                                                                 
		fi                                                                                                                                                                                                                
		let j=$i+1                                                                                                                                                                                            
#            echo "j=$j, index=$index"                                                                                                                                                                                        
		# 当前目录是最后一级目录                                                                                                                                                                                          
		if [ ! $j -lt $index ]; then                                                                                                                                                                                          
			# 除去文件前缀                                                                                                                                                                                                    
			length=0                                                                                                                                                                                                          
			for (( i=0; i<$index; i++)); do                                                                                                                                                                                       
				if [ $i -eq 0 ]; then                                                                                                                                                                                                 
					let length=${#array[$i]}+length                                                                                                                                                                               
				else                                                                                                                                                                                                                  
					let length=${#array[$i]}+length                                                                                                                                                                                   
					let length=$length+1                                                                                                                                                                                          
				fi                                                                                                                                                                                                            
			done                                                                                                                                                                                              
#                echo "length=$length"                                                                                                                                                                                            
			let length=$length+1                                                                                                                                                                                              
			r=${line:$length:${#line}}                                                                                                                                                                                        
#			     echo "r=$r"                                                                                                                                                                                                       
			line=$2$slash$line                                                                                                                                                                                                
			string=$string$slash$r                                                                                                                                                                                            
			echo "cp $line $string"                                                                                                                                                                                           
			cp $line $string                                                                                                                                                                                              
		fi                                                                                                                                                                                                            
	done                                                                                                                                                                                                              
	if [ $index -eq 0 ]; then                                                                                                                                                                                             
		line=$2$slash$line                                                                                                                                                                                    
#            echo "cp $line $parent_dir_name$slash"                                                                                                                                                                           
		cp $line $parent_dir_name$slash                                                                                                                                                                               
	fi                                                                                                                                                                                                                
	string=""                                                                                                                                                                                                         
	index=0                                                                                                                                                                                                       
	done                                                                                                                                                                                                          
fi

